package com.wave.traffic.task;

import com.wave.traffic.NetSDKDemo.FMSGCallBack_V31;
import com.wave.traffic.NetSDKDemo.FMSGCallBack_V32;
import com.wave.traffic.enums.NetControllerEnum;
import com.wave.traffic.listener.InitKND;
import com.wave.traffic.util.SocketClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @Description 雷视摄像机监控任务
 * @Author linhui
 * @Date 2022/9/28 10:00
 */
public class HKCameraTask extends Thread {
    private final static Logger logger = LoggerFactory.getLogger(HKCameraTask.class);

    public static BlockingQueue<Map<Socket, String>> blockingQueue1 = new ArrayBlockingQueue<>(1024 * 1024);

    public static BlockingQueue<Map<Socket, String>> blockingQueue2 = new ArrayBlockingQueue<>(1024 * 1024);

    public static BlockingQueue<Map<Socket, String>> blockingQueue3 = new ArrayBlockingQueue<>(1024 * 1024);

    @Autowired
    private ServerInfo serverInfo;
    public volatile static boolean hasCar = true;

    public HKCameraTask(ServerInfo serverInfo) {
        this.serverInfo = serverInfo;
    }

    @Override
    public void run() {
        try {
            //等待3个康耐德设备都上线了
            while (InitKND.socketMap.size()<3){
                Thread.sleep(1000);
            }
            Socket netController1 = InitKND.socketMap.get(serverInfo.getServerIP1());
            Socket netController2 = InitKND.socketMap.get(serverInfo.getServerIP2());
            Socket netController3 = InitKND.socketMap.get(serverInfo.getServerIP3());
            // 打开红外感应
            logger.info("3个康耐德设备都上线了,开始发送命令打开红外");
            hasCar = false;
            //SocketClientUtil.sendData(netController1, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            Map<Socket, String> map = new ConcurrentHashMap<>();
            map.put(netController1, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            blockingQueue1.put(map);

            //SocketClientUtil.sendData(netController2, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            map = new ConcurrentHashMap<>();
            map.put(netController2, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            blockingQueue2.put(map);

            //SocketClientUtil.sendData(netController3, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            map = new ConcurrentHashMap<>();
            map.put(netController3, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
            blockingQueue3.put(map);

            while (true) {

                netController1 = InitKND.socketMap.get(serverInfo.getServerIP1());
                netController2 = InitKND.socketMap.get(serverInfo.getServerIP2());
                netController3 = InitKND.socketMap.get(serverInfo.getServerIP3());

//                logger.info("BBBBBBBBBBB");
//            logger.info("摄像头1检测车辆状态:{},摄像头2检测车辆状态:{}", FMSGCallBack_V31.isVehicle(), FMSGCallBack_V32.isVehicle());
                //有车就关闭红外感应，无车则打开红外感应
                if (FMSGCallBack_V31.isVehicle() || FMSGCallBack_V32.isVehicle()) {
                    logger.info("有车,休眠15秒开始");
                    if(netController1 != null){
                        //SocketClientUtil.sendData(netController1, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController1, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        blockingQueue1.put(map);
                    }
                    if(netController2 != null){
                        //SocketClientUtil.sendData(netController2, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController2, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        blockingQueue2.put(map);
                    }
                    if(netController3 != null){
                        //SocketClientUtil.sendData(netController3, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController3, NetControllerEnum.WRITE_DO4_OPEN_COMMAND.getCode());
                        blockingQueue3.put(map);
                    }
                    hasCar = true;
                    try {
                        Thread.sleep(1000 * 15);
                        logger.info("有车，休眠15秒结束");
                    } catch (InterruptedException e) {
                        logger.error("HKCameraTask sleep error:[{}]", e.getMessage(), e);
                    }
                    FMSGCallBack_V31.resetVehicle();
                    FMSGCallBack_V32.resetVehicle();

                    // 打开红外感应
                    hasCar = false;
                    if(netController1 != null){
                        //SocketClientUtil.sendData(netController1, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController1, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        blockingQueue1.put(map);
                    }
                    if(netController2 != null){
                        //SocketClientUtil.sendData(netController2, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController2, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        blockingQueue2.put(map);
                    }
                    if(netController3 != null){
                        //SocketClientUtil.sendData(netController3, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        map = new ConcurrentHashMap<>();
                        map.put(netController3, NetControllerEnum.WRITE_DO4_CLOSE_COMMAND.getCode());
                        blockingQueue3.put(map);
                    }

                    continue;
                }
//            logger.info("是否有车：{}",hasCar);
                Thread.sleep(100);
            }
        } catch (Exception e) {
            logger.error("HKCameraTask error:[{}]", e.getMessage(), e);
        }

    }
}
